#include "Player.h"
#include "Game.h"

void Player::update(float frameTime)
{
	const InputHandler* inputHandler = game->getInputHandler();
	Game* instance = (Game*)game;
	CXBOXController* Player1;
	Player1 = new CXBOXController(1);	
	
	// Set up input responses
	if (inputHandler->isKeyDown(DIK_LEFT))
	{
		anims->playAnimation("Run");
		particle.velocity.x = -100.0f;
		flip = true;
	}
	else if (inputHandler->isKeyDown(DIK_RIGHT))
	{
		anims->playAnimation("Run");
		particle.velocity.x = 100.0f;
		flip = false;
	}
	else if (inputHandler->isKeyDown(DIK_UP))
	{
		anims->playAnimation("Run");
		particle.velocity.y = -100.0f;
	}
	else if (inputHandler->isKeyDown(DIK_DOWN))
	{
		anims->playAnimation("Run");
		particle.velocity.y = 100.0f;
	}
	else if (inputHandler->isKeyDown(DIK_SPACE))
	{
		anims->playAnimation("Target");

	}

	// for xbox360 controller 
	else if (Player1->IsConnected())
	{
		if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
		{
			anims->playAnimation("Run");
		    particle.velocity.x = -100.0f;
		    flip = true; 
		}
		else if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
		{
			anims->playAnimation("Run");
		    particle.velocity.x = 100.0f;
		    flip = false;
		}
		else if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
		{
			anims->playAnimation("Run");
		    particle.velocity.y = -100.0f;
		}
		else if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
		{
			anims->playAnimation("Run");
		    particle.velocity.y = 100.0f;
		}
		else if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
		{
			anims->playAnimation("Target");
		}

		/*else if(Player1->GetState().Gamepad.sThumbLX & XINPUT_GAMEPAD_LEFT_THUMB)
		{
			anims->playAnimation("Run");
		    particle.velocity.x = -100.0f;
		    flip = true; 
		}
		else if(Player1->GetState().Gamepad.sThumbLX & XINPUT_GAMEPAD_RIGHT_THUMB)
		{
			anims->playAnimation("Run");
		    particle.velocity.x = 100.0f;
		    flip = false;
		}*/
	}

	
	else
		anims->playAnimation("Idle");

	
		

	
	
	

	/** Update animations and particle. Every frame. */
	anims->update(frameTime);
	particle.update(frameTime);

	position = particle.position;
}

void Player::draw(ID3DXSprite* sprite)
{
	//First we work out the centre of the frame
	D3DXVECTOR2 centre;
	centre.x = (float)(anims->getCurrentAnimation()->getFrameWidth() / 2);
	centre.y = (float)(anims->getCurrentAnimation()->getFrameHeight() / 2);

	/* Now we create a transformation matrix. If flipped, we scale X by -1
	 * we also use the position vector in our transaction */
	D3DXMATRIX mat;
	if(flip)
		D3DXMatrixTransformation2D(&mat, &centre, 0,
								   &D3DXVECTOR2(-1, 1), 0, 0,
								   &D3DXVECTOR2(position.x, position.y));
	else 
		D3DXMatrixTransformation2D(&mat, 0, 0, 0, 0, 0,
								   &D3DXVECTOR2(position.x, position.y));

	//Now we use this transformation to draw with, we set the draw position to 0 to avoid issues
	
	anims->draw(sprite, D3DXVECTOR3(0, 0, 0), mat);
}

void Player::handleMessage(Message message)
{
	// HANDLE MESSAGES...
}